package com.ifnk.portaljava.hlyt.kpi;

import com.ifnk.portaljava.dapWeb.advance.DBAccess;
import com.ifnk.portaljava.hlyt.kpi.beans.GnFlow;
import com.ifnk.portaljava.hlyt.kpi.beans.GnSuccess;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class DataOfGn {

	public ArrayList<GnSuccess> getActivation(String condtion) {
		ArrayList<GnSuccess> DataActArr = new ArrayList<GnSuccess>();
		String[] arr = condtion.split(",");
		String startTime = arr[0];
		String endTime = arr[1];
		int unit = Integer.valueOf(arr[2]);
		Date oldTime = new Date();
		Calendar oldCal = Calendar.getInstance();
		oldCal.set(Integer.valueOf(startTime.substring(0, 3)), Integer
				.valueOf(startTime.substring(5, 6)), Integer.valueOf(startTime
						.substring(8, 9)),
						Integer.valueOf(startTime.substring(11, 12)), Integer
						.valueOf(startTime.substring(14, 15)));
		oldTime = oldCal.getTime();
		long oldTimelong = oldTime.getTime();

		String sqlWhere = "where time > '" + startTime + "' and time < '"
		+ endTime + "' ";
		DBAccess dbQuery = new DBAccess();
		ResultSet rs = null;
		String sqlStr = " select  ";
		sqlStr += sqlOfUnit(unit);
		sqlStr += "sum(success_create_number) as create_suc,sum(request_create_number) as create_req,"
			+ "sum(success_delete_number) as delete_suc,sum(request_create_number) as delete_req,"
			+ "case when request_create_number <> 0 then success_create_number/request_create_number "
			+ "when request_create_number = 0 then 0 else 0 end as create_success_pcnt, "
			+ "case when request_create_number <> 0 then success_delete_number/request_create_number "
			+ "when request_create_number = 0 then 0 else 0 end as delete_success_pcnt "
			+ "from gnsuccess ";
		sqlStr += sqlWhere;
		sqlStr += "group by 1;";
		System.out.println(sqlStr);
		// dbQuery.queryTable("set @id = 0;");
		rs = dbQuery.queryTable(sqlStr);
		try {
			int i = 0;
			while (rs.next()) {
				GnSuccess gs = new GnSuccess();
				SimpleDateFormat df = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
				Date d = new Date();
				d = rs.getTimestamp("time");

				System.out.println(df.format(d));
				DataActArr = insertZero(DataActArr, oldTimelong,
						d.getTime() / 1000, unit,"act");
				i = DataActArr.size();
				oldTimelong = ((Date) rs.getTimestamp("time")).getTime() / 1000;
				try {
					gs.setId(++i);
					System.out.print(gs.getId());
					System.out.print("/");
				} catch (Exception e) {
					gs.setId(0);
					e.printStackTrace();
				}
				try {
					gs.setTime(rs.getString("time"));
					System.out.print(rs.getString("time"));
					System.out.print("/");
				} catch (Exception e) {
					gs.setTime("");
					e.printStackTrace();
				}
				try {
					gs.setCreateSuccessNo(rs.getInt("create_suc"));
					System.out.print(gs.getCreateSuccessNo());
					System.out.print("/");
				} catch (Exception e) {
					gs.setCreateSuccessNo(0);
					e.printStackTrace();
				}
				try {
					gs.setCreateRequestNo(rs.getInt("create_req"));
					System.out.print(gs.getCreateRequestNo());
					System.out.print("/");
				} catch (Exception e) {
					gs.setCreateRequestNo(0);
					e.printStackTrace();
				}
				try {
					gs.setCreateSuccessPcnt(rs.getDouble("create_success_pcnt"));
					System.out.print(gs.getCreateSuccessPcnt());
					System.out.print("/");
				} catch (Exception e) {
					gs.setCreateSuccessPcnt(0.0);
					e.printStackTrace();
				}
				try {
					gs.setDeleteSuccessNo(rs.getInt("delete_suc"));
					System.out.print(gs.getDeleteSuccessNo());
					System.out.print("/");
				} catch (Exception e) {
					gs.setDeleteSuccessNo(0);
					e.printStackTrace();
				}
				try {
					gs.setDeleteRequestNo(rs.getInt("delete_req"));
					System.out.print(gs.getDeleteRequestNo());
					System.out.print("/");
				} catch (Exception e) {
					gs.setDeleteRequestNo(0);
					e.printStackTrace();
				}
				try {
					gs
					.setDeleteSuccessPcnt(rs
							.getDouble("delete_success_pcnt"));
					System.out.print(gs.getCreateSuccessPcnt());
					System.out.println("/");
				} catch (Exception e) {
					gs.setDeleteSuccessPcnt(0.0);
					e.printStackTrace();
				}
				DataActArr.add(gs);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return DataActArr;
		}
	}

	public ArrayList getFlow(String condiotn) {
		double peakUpP = 0.0;
		int peakUpPid = 0;
		String peakUpPtime = "";
		double peakUpB = 0.0;
		int peakUpBid = 0;
		String peakUpBtime = "";
		double peakDownP = 0.0;
		int peakDownPid = 0;
		String peakDownPtime = "";
		double peakDownB = 0.0;
		int peakDownBid = 0;
		String peakDownBtime = "";
		ArrayList<GnFlow> DataFlowArr = new ArrayList();
		String[] arr = condiotn.split(",");
		String startTime = arr[0];
		String endTime = arr[1];
		int unit = Integer.valueOf(arr[2]);
		Date oldTime = new Date();
		Calendar oldCal = Calendar.getInstance();
		oldCal.set(Integer.valueOf(startTime.substring(0, 3)), Integer
				.valueOf(startTime.substring(5, 6)), Integer.valueOf(startTime
						.substring(8, 9)),
						Integer.valueOf(startTime.substring(11, 12)), Integer
						.valueOf(startTime.substring(14, 15)));
		oldTime = oldCal.getTime();
		long oldTimelong = oldTime.getTime();

		String sqlWhere = "where time > '" + startTime + "' and time < '"
		+ endTime + "' ";
		DBAccess dbQuery = new DBAccess();
		ResultSet rs = null;
		String sqlStr = " select  ";
		sqlStr += sqlOfUnit(unit);
		sqlStr += "sum(up_package_rate)/count(up_package_rate) as up_p_rate, "
			+ "sum(up_byte_rate)/count(up_byte_rate) as up_b_rate, "
			+ "sum(down_package_rate)/count(down_package_rate) as down_p_rate, "
			+ "sum(down_byte_rate)/count(down_byte_rate) as down_b_rate "
			+ "from gnflow ";
		sqlStr += sqlWhere;
		sqlStr += "group by 1;";
		System.out.println(sqlStr);
		rs = dbQuery.queryTable(sqlStr);
		try {
			int i = 0;
			while (rs.next()) {
				GnFlow gf = new GnFlow();
				Date d = new Date();
				d = rs.getTimestamp("time");
				DataFlowArr = insertZero(DataFlowArr, oldTimelong,
						d.getTime() / 1000, unit,"flow");
				i = DataFlowArr.size();
				oldTimelong = ((Date) rs.getTimestamp("time")).getTime() / 1000;
				try {
					gf.setId(++i);
					System.out.print(gf.getId());
					System.out.print("/");
				} catch (Exception e) {
					gf.setId(0);
					e.printStackTrace();
				}
				try {
					gf.setTime(rs.getString("time"));
					System.out.print(rs.getString("time"));
					System.out.print("/");
				} catch (Exception e) {
					gf.setTime("");
					e.printStackTrace();
				}
				try {
					gf.setUpPackageRate(rs.getDouble("up_p_rate"));
					if(peakUpP < gf.getUpPackageRate()){
						peakUpP = gf.getUpPackageRate();
						peakUpPid = gf.getId();
						peakUpPtime = gf.getTime();
					}
					System.out.print(gf.getUpPackageRate());
					System.out.print("/");
				} catch (Exception e) {
					gf.setUpPackageRate(0);
					e.printStackTrace();
				}
				try {
					gf.setUpByteRate(rs.getDouble("up_b_rate"));
					if(peakUpB < gf.getUpByteRate()){
						peakUpB = gf.getUpByteRate();
						peakUpBid = gf.getId();
						peakUpBtime = gf.getTime();
					}
					System.out.print(gf.getUpByteRate());
					System.out.print("/");
				} catch (Exception e) {
					gf.setUpByteRate(0);
					e.printStackTrace();
				}

				try {
					gf.setDownPackageRate(rs.getDouble("down_p_rate"));
					if(peakDownP < gf.getDownPackageRate()){
						peakDownP = gf.getDownPackageRate();
						peakDownPid = gf.getId(); 
						peakDownPtime = gf.getTime();
					}
					System.out.print(gf.getDownPackageRate());
					System.out.print("/");
				} catch (Exception e) {
					gf.setDownPackageRate(0);
					e.printStackTrace();
				}

				try {
					gf.setDownByteRate(rs.getDouble("down_b_rate"));
					if(peakDownB < gf.getDownByteRate()){
						peakDownB = gf.getDownByteRate();
						peakDownBid = gf.getId();
						peakDownBtime = gf.getTime();
					}
					System.out.print(gf.getDownByteRate());
					System.out.println("/");
				} catch (Exception e) {
					gf.setDownByteRate(0);
					e.printStackTrace();
				}

				DataFlowArr.add(gf);
			}
			GnFlow gftmp = new GnFlow();
			gftmp.setId(peakUpPid);
			gftmp.setTime(peakUpPtime);
			gftmp.setUpPackageRate(peakUpP);
			DataFlowArr.add(gftmp);
			
			GnFlow gftmp1 = new GnFlow();
			gftmp1.setId(peakUpBid);
			gftmp1.setTime(peakUpBtime);
			gftmp1.setUpByteRate(peakUpB);
			DataFlowArr.add(gftmp1);
			
			GnFlow gftmp2 = new GnFlow();
			gftmp2.setId(peakDownPid);
			gftmp2.setTime(peakDownPtime);
			gftmp2.setDownPackageRate(peakDownP);
			DataFlowArr.add(gftmp2);
			
			GnFlow gftmp3 = new GnFlow();
			gftmp3.setId(peakDownBid);
			gftmp3.setTime(peakDownBtime);
			gftmp3.setDownByteRate(peakDownB);
			DataFlowArr.add(gftmp3);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return DataFlowArr;
	}

	public ArrayList insertZero(ArrayList arr, long oldT, long newT, int unit,String name) {
		if (arr.size() == 0) {
			return arr;
		}
		long interval = newT - oldT;
		switch (unit) {
		case 1:
			if (interval == 60) {
				break;
			} else {
				int k = (int) (interval / 60);
				arr = insertZeroToArr(arr, k, oldT, 60,name);
			}
			break;
		case 5:
			if (interval == 300) {
				break;
			} else {
				int k = (int) (interval / 300);
				arr = insertZeroToArr(arr, k, oldT, 300,name);
			}
			break;
		case 10:
			if (interval == 600) {
				break;
			} else {
				int k = (int) (interval / 600);
				arr = insertZeroToArr(arr, k, oldT, 600,name);
			}
			break;
		case 15:
			if (interval == 900) {
				break;
			} else {
				int k = (int) (interval / 900);
				arr = insertZeroToArr(arr, k, oldT, 900,name);
			}
			break;
		case 30:
			if (interval == 1800) {
				break;
			} else {
				int k = (int) (interval / 1800);
				arr = insertZeroToArr(arr, k, oldT, 1800,name);
			}
			break;
		case 60:
			if (interval == 3600) {
				break;
			} else {
				int k = (int) (interval / 3600);
				arr = insertZeroToArr(arr, k, oldT, 3600,name);
			}
		case 70:
			if (interval == 1440 * 60) {
				break;
			} else {
				int k = (int) (interval / (1440 * 60));
				arr = insertZeroToArr(arr, k, oldT, 1440 * 60,name);
			}
			break;
		case 80: // 月单
			Calendar calNew = Calendar.getInstance();
			Date dtmp = new Date();
			dtmp.setTime(newT*1000);
			//SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//			System.out.println(df.format(dtmp));
			calNew.setTime(dtmp);
			Calendar calOld = Calendar.getInstance();
			dtmp.setTime(oldT*1000);
//			System.out.println(df.format(dtmp));
			calOld.setTime(dtmp);
			int k = calNew.get(Calendar.MONTH) - calOld.get(Calendar.MONTH);
			while (k > 1) {
				calOld.add(Calendar.MONTH,1);
				dtmp = calOld.getTime();
				arr = insertZeroToArr(arr, 2, dtmp.getTime()/1000, 0,name);
				k--;
			}
			break;
		default:
			break;
		}
		return arr;
	}

	public ArrayList insertZeroToArr(ArrayList arr, int k, long oldT, int unit,String name) {
		if(name == "act") {
			while (k > 1) {
				GnSuccess gs = new GnSuccess();
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date dd = new Date();
				dd.setTime(oldT * 1000);
//				System.out.println(df.format(dd));
				oldT = oldT + unit;
				dd.setTime(oldT * 1000);

				gs.setId(((GnSuccess) arr.get(arr.size() - 1)).getId() + 1);
//				System.out.print(gs.getId() + "/");
				gs.setTime(df.format(dd));
//				System.out.print(gs.getTime() + "/");
				gs.setCreateRequestNo(0);
//				System.out.print(gs.getCreateRequestNo() + "/");
				gs.setCreateSuccessNo(0);
//				System.out.print(gs.getCreateSuccessNo() + "/");
				gs.setCreateSuccessPcnt(0.0);
//				System.out.print(gs.getCreateSuccessPcnt() + "/");
				gs.setDeleteRequestNo(0);
//				System.out.print(gs.getDeleteRequestNo() + "/");
				gs.setDeleteSuccessNo(0);
//				System.out.print(gs.getDeleteSuccessNo() + "/");
				gs.setDeleteSuccessPcnt(0.0);
//				System.out.println(gs.getDeleteSuccessPcnt() + "/");
				arr.add(gs);
				k--;
			}
		}
		if (name == "flow") {
			while (k > 1) {
				GnFlow gf = new GnFlow();
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date dd = new Date();
				dd.setTime(oldT * 1000);
//				System.out.println(df.format(dd));
				oldT = oldT + unit;
				dd.setTime(oldT * 1000);

				gf.setId(((GnFlow) arr.get(arr.size() - 1)).getId() + 1);
//				System.out.print(gf.getId() + "/");
				gf.setTime(df.format(dd));
//				System.out.print(gf.getTime() + "/");
				gf.setUpPackageRate(0.0);
//				System.out.print(gf.getUpPackageRate() + "/");
				gf.setUpByteRate(0.0);
//				System.out.print(gf.getUpByteRate() + "/");
				gf.setDownPackageRate(0.0);
//				System.out.print(gf.getDownPackageRate() + "/");
				gf.setDownByteRate(0.0);
//				System.out.print(gf.getDownByteRate() + "/");
				arr.add(gf);
				k--;
			}
		}
		return arr;
	}

	public String sqlOfUnit(int unit) {
		String sql = "";
		switch (unit) {
		case 1: // 1分钟
			sql = "FROM_UNIXTIME(UNIX_TIMESTAMP(time)- UNIX_TIMESTAMP(time) mod 60) as time, ";
			break;
		case 5: // 5分钟
			sql = "FROM_UNIXTIME(UNIX_TIMESTAMP(time)- UNIX_TIMESTAMP(time) mod 300) as time, ";
			break;
		case 10: // 10分钟
			sql = "FROM_UNIXTIME(UNIX_TIMESTAMP(time)- UNIX_TIMESTAMP(time) mod 600) as time, ";
			break;
		case 15: // 15分钟
			sql = "FROM_UNIXTIME(UNIX_TIMESTAMP(time)- UNIX_TIMESTAMP(time) mod 900) as time, ";
			break;
		case 30: // 30分钟
			sql = "FROM_UNIXTIME(UNIX_TIMESTAMP(time)- UNIX_TIMESTAMP(time) mod 1800) as time, ";
			break;
		case 60: // 小时
			sql = "FROM_UNIXTIME(UNIX_TIMESTAMP(time)- UNIX_TIMESTAMP(time) mod 3600) as time,";
			break;
		case 70: // 天
			sql = "FROM_UNIXTIME(UNIX_TIMESTAMP(time)- UNIX_TIMESTAMP(time) mod 86400) as time,";
			break;
		case 80: // 月
			sql = "FROM_UNIXTIME(UNIX_TIMESTAMP(DATE_FORMAT(DATE_SUB(TIME,INTERVAL (DAY(time)-1) DAY),'%Y%m%d'))) as time, ";
			break;
		default:
			sql = "time,'', ";
			break;
		}

		return sql;
	}

	public static void main(String[] arg) {
//		DataOfGnsuccess dog = new DataOfGnsuccess();
//		dog.getActivation("2010-10-22 8:10,2011-10-23 11:10,80");
		/*
		 * Date startTime = new Date();
		 * 
		 * SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 * 
		 * Date date=new Date(); Calendar cal=Calendar.getInstance();
		 * cal.set(2010,0,1,0,0,0); Date st = cal.getTime();
		 * cal.set(2010,0,2,0,0,0); Date et = cal.getTime(); int i = 0;
		 * while(st.getTime()<et.getTime()){ i++; System.out.print(i+" ");
		 * System.out.println(df.format(st)); st.setTime(st.getTime()+60000);
		 * 
		 * }
		 */

	}
}
